package dxunderground.winampController.Server;

import java.io.File;

import dxunderground.winampController.Common.GUI;
import dxunderground.winampController.Server.GUI.ButtonActions;

public class Protocol {
	private GUI gui;
	private ButtonActions buttonActions;

	public Protocol(GUI gui, ButtonActions buttonActions) {
		this.gui = gui;
		this.buttonActions = buttonActions;
	}

	public String[] processInput(String theInput) {
		String[] theOutput = { "" };
		if (theInput != null) {
			if (theInput.equals("Get Playlist")) {
				Object[] songs = gui.listModel.toArray();
				theOutput = new String[songs.length + 2];
				theOutput[0] = "<Songs>";
				for (int i = 0; i < songs.length; i++) {
					theOutput[i + 1] = ((File) songs[i]).getAbsolutePath();
				}
				theOutput[theOutput.length - 1] = "</Songs>";
			} else if (theInput.indexOf("<Add>") > -1) {
				String song = theInput.substring(theInput.indexOf(">") + 1, theInput.indexOf("</Add>"));
				if (!this.buttonActions.play(song)) {
					theOutput[0] = "INVALID";
				}
			} else if (theInput.equals("<||>/>")) {
				this.buttonActions.playPause();
			} else if (theInput.equals("<[]/>")) {
				this.buttonActions.stop();
			} else if (theInput.equals("<>>/>")) {
				this.buttonActions.next();
			} else if (theInput.equals("<<</>")) {
				this.buttonActions.previous();
			} else if (theInput.equals("__END__")) {
				theOutput[0] = theInput;
			}
		}
		return theOutput;
	}
}
